/*
 * @Author: huangtianyang 916072572@qq.com
 * @Date: 2023-09-01 09:15:50
 * @LastEditors: huangtianyang
 * @LastEditTime: 2023-10-09 10:28:35
 * @FilePath: \cxl-admin\src\pages\company\admin\list\index.ts
 */
import { BaseData, BaseMethod } from '@/utils/basc-data'
import * as tableConfig from './config.json'
import { adminDelete, adminEnable, adminList, adminParams } from '@/apis/company/admin'
import { reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Message, MessageBox } from '@element-plus/icons-vue'

export default class CompanyAdminData extends BaseData implements BaseMethod {
    companyId = ''
    phone = ''
    status = '-1'
    dialogData = reactive({
        visible: false,
        type: 'create',
        isEditAdmin: false,
        config: {}
    })
    constructor() {
        super(tableConfig)

        if (this.uStore.isSuperAdmin) {
            this.pageData.search.unshift(['公司名称|编号'])
        }

        this.getData()
    }
    getData = (arg?: any) => {
        const obj: adminParams = {
            current: this.pageData.pagination.pageNo,
            size: this.pageData.pagination.pageSize
        }
        if (this.companyId) {
            obj.companyId = this.companyId
        }
        if (this.phone) {
            obj.phone = this.phone
        }
        console.log(this.status)
        if (this.status != '-1' && this.status) {
            obj.status = Number(this.status)
        }

        console.log(obj)
        adminList(obj).then(res => {
            if (res.data.value === 200) {
                this.pageData.table.data.length = 0
                this.pageData.pagination.total = res.data.data.total
                const arr: any[] = res.data.data.records

                arr.forEach((element: any) => {
                    element.status = element.status ? true : false
                    this.pageData.table.data.push(element)
                })
            }
        })
    }
    onSearch = (arg?: any) => {
        console.log(arg)
        this.companyId = arg.companyId
        this.phone = arg.phone
        this.status = arg.status

        this.getData()
    }
    onReset = (arg?: any) => {
        this.companyId = ''
        this.phone = ''
        this.status = '-1'

        this.getData()
    }
    onPaginationChange = (arg?: any) => {
        this.pageData.pagination.pageNo = arg
        this.getData()
    }
    onPaginationSize = (arg?: any) => {
        this.pageData.pagination.pageNo = 1
        this.pageData.pagination.pageSize = arg
        this.getData()
    }
    onOperation = (arg?: any) => {
        const { type, data } = arg
        console.log(arg)
        if (type === 'create') {
            this.dialogData.config = {}
            this.dialogData.type = type
            this.dialogData.visible = true
        } else if (type === 'edit') {
            this.dialogData.config = data
            this.dialogData.type = type
            this.dialogData.visible = true
        } else if (type === 'delete') {
            ElMessageBox.confirm(`确定要删除【${data.name}】账号吗？`, '删除', {
                confirmButtonText: '确定',
                cancelButtonText: '取消'
            })
                .then(() => {
                    adminDelete(data.id).then(res => {
                        if (res.data.value === 200) {
                            ElMessage.success('操作成功')
                            this.getData()
                        }
                    })
                })
                .catch(() => {})
        } else if (type === 'switch') {
            adminEnable(Number(data.status), data.id).then(res => {
                console.log(res)
                if (res.data.value === 200) {
                    ElMessage.success(data.status ? '账号启用成功！' : '账号禁用成功')
                } else {
                    ElMessage.error(res.data.desc)
                }
                this.getData()
            })
        }
    }
    onDialogClose = () => {
        this.dialogData.visible = false
    }
}
